package com.konsung.ksdc.manager.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.RegisterEmpNetBean;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.PreferenceUtils;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.common.utils.ToastUtils;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 账号注册页面
 * Created by Administrator on 2016/3/7 0007.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.btn_reg)
    Button btnReg;
    @Bind(R.id.et_username)
    EditText etUserName;
    @Bind(R.id.et_empname)
    EditText etEmpName;
    @Bind(R.id.et_password)
    EditText etPwd;
    @Bind(R.id.et_pwd_confirm)
    EditText etPwdConfirm;
    @Bind(R.id.et_address)
    EditText etAddr;
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;

    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        phone = getIntent().getStringExtra("phone");

        ButterKnife.bind(this);

        initView();

        btnReg.setOnClickListener(this);
    }

    @Override
    protected String activityDesc() {
        return getString(R.string.page_register);
    }

    /**
     * 初始化注册页面
     */
    private void initView() {
        tvTitle.setText(R.string.title_edit_user_data);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_reg) {
            String userName = etUserName.getText().toString().trim();
            String empName = etEmpName.getText().toString().trim();
            String pwd = etPwd.getText().toString().trim();
            String addr = etAddr.getText().toString().trim();
            if (StringUtils.isEmpty(userName)) {
                Toast.makeText(this, R.string.please_input_username, Toast.LENGTH_SHORT).show();
                return;
            }
            if (StringUtils.isAllNumber(userName)) {
                Toast.makeText(this, R.string.tip_username_all_number, Toast.LENGTH_SHORT).show();
                return;
            }
            if (StringUtils.isEmpty(pwd)) {
                Toast.makeText(this, R.string.please_set_password, Toast.LENGTH_SHORT).show();
                return;
            }
            // 密码设置至少6位字符
            if (pwd.length() < 6) {
                Toast.makeText(this, R.string.password_less_six, Toast.LENGTH_SHORT).show();
                return;
            }
            if (!etPwdConfirm.getText().toString().trim().equals(pwd)) {
                Toast.makeText(this, R.string.tip_pwd_comfirm_error, Toast
                        .LENGTH_SHORT)
                        .show();
                return;
            }

            register(new RegisterEmpNetBean(empName, phone, addr, new
                    RegisterUserNetBean(empName, userName, pwd)));
        }
    }

    /**
     * 网络请求 register
     * @param bean 注册Bean
     */
    private void register(final RegisterEmpNetBean bean) {
        if (!RequestUtils.checkNetwork(this)) {
            return;
        }
        RequestUtils.register(bean, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                ToastUtils.toastContent(RegisterActivity.this,
                        getString(R.string.chat_unconnect));
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                if (responseBean.getResultCode().equals(Constants.REQUEST_OK)) {
                    //将注册的号码和密码储存起来
                    PreferenceUtils.putString(RegisterActivity.this, Constants.USERNAME,
                            bean.getUser().getUserName());
                    PreferenceUtils.putString(RegisterActivity.this, Constants.PASSWORD, bean
                            .getUser().getPassword());
                    ToastUtils.toastContent(RegisterActivity.this, getString(R.string
                            .register_success));
                    finish();
                } else {
                    Toast.makeText(RegisterActivity.this, R.string.register_fail,
                            Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }
}
